package com.jike.noobmoney.mvp.view.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.TuiJianEntity;
import com.jike.noobmoney.net.RetrofitHelper;
import com.jike.noobmoney.util.SPUtils;
import com.jike.noobmoney.util.ToastUtils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Create by xuke on 2019/03/03
 * 充值中心
 */
public class YaoqingActivity extends BaseActivity {

    @BindView(R.id.tv_recode)
    TextView tvRecode;
    @BindView(R.id.tv_sum_number)
    TextView tvSumNumber;
    @BindView(R.id.tv_today_number)
    TextView tvTodayNumber;
    @BindView(R.id.tv_sum_money)
    TextView tvSumMoney;
    @BindView(R.id.tv_today_money)
    TextView tvTodayMoney;
    @BindView(R.id.btn_share)
    Button btnShare;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    private String userid;
    private String recode;

    @Override
    protected int layoutResID() {
        return R.layout.activity_yaoqing;
    }

    @Override
    protected void initData() {
        tvTitle.setText("邀请好友瓜分红包");
        recode = SPUtils.getInstance().getString(ConstantValue.SpType.recode); //推荐
        String textStr = "我的推荐码: <font color='#FFFFFF'>" + recode + "</font>";
        tvRecode.setText(Html.fromHtml(textStr));

        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);

        getData();


    }

    /**
     * 获取数据
     */
    private void getData() {
        Call<TuiJianEntity> getincome = RetrofitHelper.getInstance().getincome(userid);
        getincome.enqueue(new Callback<TuiJianEntity>() {
            @Override
            public void onResponse(Call<TuiJianEntity> call, Response<TuiJianEntity> response) {
                if (response != null) {
                    TuiJianEntity body = response.body();
                    if (body != null) {
                        if ("100".equals(body.getCode())) {
                            initView(body.getInvite());
                        } else {
                            ToastUtils.showShortToastSafe(body.getRinfo());
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<TuiJianEntity> call, Throwable t) {
                ToastUtils.showShortToastSafe(t.getMessage());
            }
        });

    }

    /**
     * 更新视图
     */
    private void initView(TuiJianEntity.InviteBean invite) {
        if (tvSumNumber != null) {
            tvSumNumber.setText(invite.getSumnumber() + "人");
        }

        if (tvTodayNumber != null) {
            String todayNumber = "今日邀请好友: <font color='#D51918'>" + invite.getNumber() + "</font>人";
            tvTodayNumber.setText(Html.fromHtml(todayNumber));
        }

        if (tvSumMoney != null) {
            tvSumMoney.setText(invite.getSummoney() + "豆");
        }

        if (tvTodayMoney != null) {
            String todayMoney = "今日邀请收益: <font color='#D51918'>" + invite.getTodaymoney() + "</font>豆";
            tvTodayMoney.setText(Html.fromHtml(todayMoney));
        }
    }


    /**
     * 点击分享
     */
    public void recommend() {
        String recode = SPUtils.getInstance().getString(ConstantValue.SpType.recode); //分享
        String taskurl = SPUtils.getInstance().getString(ConstantValue.SpType.taskurl);
        String url = !TextUtils.isEmpty(taskurl) ? taskurl + "?recode=" + recode : ConstantValue.COMMENTURLCOPY + recode;
        UMWeb web = new UMWeb(url);
        web.setTitle("小白赚钱");
        web.setThumb(new UMImage(context, R.mipmap.ic_launcher));
        web.setDescription(getString(R.string.text_yaoqing1));
        ShareAction shareAction = new ShareAction(this);
        shareAction.withMedia(web);
        shareAction.setDisplayList(SHARE_MEDIA.QZONE, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE);
        shareAction.setCallback(shareListener);
        shareAction.open();
    }


    private UMShareListener shareListener = new UMShareListener() {

        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            ToastUtils.showShortToastSafe("分享成功了");
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            ToastUtils.showShortToastSafe("分享失败了");

        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            ToastUtils.showShortToastSafe("分享取消了");
        }
    };


    @OnClick({R.id.iv_back, R.id.tv_recode, R.id.btn_share, R.id.ll_invite_list})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_recode:
                copyString(recode);
                break;
            case R.id.btn_share:
                recommend();
                break;
            case R.id.ll_invite_list:
                startActivity(new Intent(context, InviteListActivity.class));
                break;

        }
    }

    /**
     * 复制
     */
    private void copyString(String content) {
        ClipboardManager cm = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData mClipData = ClipData.newPlainText("Label", content);
        if (cm != null) {
            cm.setPrimaryClip(mClipData);
        }
        ToastUtils.showShortToastSafe("推荐码复制成功");

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }


}
